<?php
if(empty($_GET['query'])) die();

function valid_function_name($string) {
	return strspn($string, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789') === strlen($string);
}

$functions = get_defined_functions();
$functions = $functions['internal'];

$hits = array();
$queryLength = strlen($_GET['query']);

foreach($functions as $function) {
	if(substr($function, 0, $queryLength) == $_GET['query']) {
		if(!valid_function_name($function)) continue;
		
		$refFunction = new ReflectionFunction($function);
		
		$parameters = array();
		foreach($refFunction->getParameters() as $parameter) {
			$hint = '';
			if($parameter->isOptional()) $hint .= '(optional) ';
			if($parameter->getClass())	$hint .= get_class($parameter->getClass()) . ' ';
			if($parameter->isPassedByReference())	$hint .= '&';
			$hint .= '$' . $parameter->getName();
			
			$parameters[] = $hint;
		}
		
		$hits[] = sprintf('{
			value: \'%s\',
			hint: \'%s\'
		}', $function, addslashes($function . '(' . implode(', ', $parameters) . ')'));
	}
}
?>
<?=$_GET['callback']?>([
	<?php echo implode(',' . PHP_EOL, $hits) ?>
]);